﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace TestPrint.PrintHelp
{
    /// <summary>
    /// EMF文件回放操作类
    /// </summary>
    public class MetaFile
    {
        /// <summary>
        /// EMF回放
        /// </summary>
        public static byte[] Play(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(bytes, 0, bytes.Length);
            ms.Position = 0;
            Metafile mf = new Metafile(ms);
            return Play(mf);
        }
        /// <summary>
        /// EMF回放
        /// </summary>
        public static byte[] Play(Metafile matafile)
        {
            MemoryStream ms = null;
            Bitmap bit = new Bitmap(matafile.Width, matafile.Height);
            using (Graphics gs = Graphics.FromImage(bit))
            {
                ms = new MemoryStream();
                Rectangle rt = new Rectangle(0, 0, matafile.Width, matafile.Height);
                Metafile mf = new Metafile(ms, gs.GetHdc(), rt, MetafileFrameUnit.Pixel, EmfType.EmfOnly);
                Graphics g = Graphics.FromImage(mf);
                g.DrawImage(matafile, rt);
                g.Dispose();
                mf.Dispose();
                byte[] bytes = ms.ToArray();
                ms.Dispose();
                ms.Close();
                return bytes;
            }
        }
    }
}
